package cn.idongjia.kuaidi.repository;


import cn.idongjia.kuaidi.Const;
import cn.idongjia.kuaidi.common.MongoUtils;
import cn.idongjia.kuaidi.mapper.ShipMapper;
import cn.idongjia.kuaidi.pojos.TrackMessage;
import cn.idongjia.kuaidi.util.HttpConnectionManager;
import cn.idongjia.kuaidi.util.Utils;
import cn.idongjia.log.Log;
import cn.idongjia.log.LogFactory;
import cn.idongjia.tianji.pojos.ShipCompany;
import cn.idongjia.tianji.pojos.Shipment;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongjia_lj on 16/4/8.
 */
@Repository("kdRepo")
public class KdRepo {
    private static final Log LOG = LogFactory.getLog(KdRepo.class);
    @Autowired
    ShipMapper shipMapper;
    private String kdUrl = "http://api.kd8.cc/posttest.aspx";
    private String secret = "kd8cctestsecret";
    private String accout = "test";

    public boolean subScribe(String ooid, String outid, String companyname) throws IOException {
        try {
            String date = Utils.getCurrentFormatDate();
            String data = companyname + "," + outid + "," + date + ";";
            HttpClient client = HttpConnectionManager.getHttpClient();
            HttpPost post = new HttpPost(kdUrl);
            post.setHeader("Content_Type", "application/x-www-form-urlencoded");
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("account", accout));
            nvps.add(new BasicNameValuePair("data", data));
            nvps.add(new BasicNameValuePair("sign", Utils.md5(secret + data + secret, true)));
            post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            HttpResponse response = client.execute(post);
            if (null != response && response.getStatusLine().getStatusCode() == Const.SUM_200 &&
                    null != response.getEntity()) {
                String result = EntityUtils.toString(response.getEntity(), "utf-8");
                LOG.info("订阅物流结果:{%s}", result);
//                if (Integer.valueOf(result) == 0) {
                    MongoUtils.add(ooid,outid,companyname);
                    return true;
//                }

            }
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
        return false;
    }

    public void update(Shipment shipment) {
        try {
            Map params = new HashMap();
            params.put("ooid", shipment.getOoid());
            params.put("outid", shipment.getNo());
            shipment=shipMapper.get(shipment.getOoid());
            ShipCompany shipCompany=shipMapper.getCompany(Long.valueOf(shipment.getCid()));
            MongoUtils.delete(shipment.getOoid());
            subScribe(shipment.getOoid(),shipment.getNo(),shipCompany.getName());
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }

    public TrackMessage getTrackMessage(String outid) {
        return null;
    }

    public String getKdUrl() {
        return kdUrl;
    }

    public void setKdUrl(String kdUrl) {
        this.kdUrl = kdUrl;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getAccout() {
        return accout;
    }

    public void setAccout(String accout) {
        this.accout = accout;
    }
}
